package cn.qiyu5522.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.constant.SystemConstants;
import cn.qiyu5522.domain.DTO.ArticleAddDTO;
import cn.qiyu5522.domain.DTO.ArticlePageQueryDTO;
import cn.qiyu5522.domain.DTO.ArticleUpdateDTO;
import cn.qiyu5522.domain.VO.ArticleQueryByIdVO;
import cn.qiyu5522.domain.VO.ArticleVO;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.entity.Article;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.domain.entity.SystemException;
import cn.qiyu5522.enums.AppHttpCodeEnum;
import cn.qiyu5522.mapper.ArticleMapper;
import cn.qiyu5522.service.ArticleService;
import cn.qiyu5522.service.ArticleTagService;
import cn.qiyu5522.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-24
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private ArticleTagService articleTagService;


    @Transactional(rollbackFor = SystemException.class)
    @Override
    public ResponseResult addArticle(ArticleAddDTO articleAddDTO) {
        // 转实体
        Article article = BeanCopyUtils.copyBean(articleAddDTO, Article.class);
        // 向文章表中 加入数据
        save(article);
        // 向 中间表 tk_article_tag表中加入 数据
        articleTagService.saveBatchByArticleTagList(article.getId(),articleAddDTO.getTags());

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getArticleListByCondition(ArticlePageQueryDTO articlePageQueryDTO) {
        // 页码
        Integer pageNum = articlePageQueryDTO.getPageNum();
        // 每页显示条数
        Integer pageSize = articlePageQueryDTO.getPageSize();
        // 标题
        String title = articlePageQueryDTO.getTitle();
        // 摘要
        String summary = articlePageQueryDTO.getSummary();
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        // 条件：文章状态正常
        queryWrapper.eq(Article::getStatus, SystemConstants.STATUS_NORMAL);
        // 标题模糊查询
        queryWrapper.like(StringUtils.hasText(title),Article::getTitle,title)
                .or()
                // 摘要模糊查询
                .like(StringUtils.hasText(summary),Article::getSummary,summary);
        // 分页
        Page<Article> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Article> records = page.getRecords();
        // 转VO
        List<ArticleVO> articleVOS = BeanCopyUtils.copyBeanList(records, ArticleVO.class);
        PageVO pageVO = new PageVO(articleVOS, page.getTotal());
        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult getArticleById(Long id) {
        Article article = getById(id);
        if(!ObjectUtil.isNotEmpty(article)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_DATA_FOUND);
        }
        // 转 VO
        ArticleQueryByIdVO articleQueryByIdVO = BeanCopyUtils.copyBean(article, ArticleQueryByIdVO.class);
        // 获取tagIds
        List<Long> tags = articleTagService.getTagIdsByArticleId(article.getId());
        articleQueryByIdVO.setTags(tags);
        return ResponseResult.okResult(articleQueryByIdVO);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult updateArticle(ArticleUpdateDTO articleUpdateDTO) {
        // 转实体
        Article article = BeanCopyUtils.copyBean(articleUpdateDTO, Article.class);
        // 更新文章
        updateById(article);
        // 根据 文章 id 更新 文章-标签中间表
        // 1. 先删除中间表原有内容
        articleTagService.removeByArticleId(article.getId());
        // 2. 再插入
        articleTagService.saveBatchByArticleTagList(article.getId(),articleUpdateDTO.getTags());
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteArticle(Long id) {
        // 删除文章  --- 逻辑删除 不用删除 文章-标签表内数据
        removeById(id);
        return ResponseResult.okResult();
    }
}
